<!--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  -->

<%@tag import="org.virtualbox_3_2.AccessMode"%>
<%@tag import="org.virtualbox_3_2.MediumType"%>
<%@tag import="org.virtualbox_3_2.MediumVariant"%>
<%@tag import="org.virtualbox_3_2.DeviceType"%>
<%@tag import="java.lang.reflect.Array"%>
<%@tag import="Beans.Responses.DataRetrievalComponents.MediumDataComponent"%>
<%@tag import="Beans.Responses.DataRetrievalComponents.MiscDataComponent"%>
<%@tag import="Beans.Responses.DataRetrievalResponse"%>
<%@tag import="Utilities.Constants.ResourceRequestType"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc" %>
<%@tag description="Prints media data." pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>

<%-- Attributes --%>
<%@attribute name="data" required="true" description="Media data response object."
             rtexprvalue="true" type="DataRetrievalResponse<MediumDataComponent>"%>

<%@attribute name="type" required="true" description="Media type (1->Hard disk; 2->DVD; 3->Floppy)."
             rtexprvalue="true" type="java.lang.Integer"%>

<%@attribute name="targetFieldName" required="false" 
             description="The name of a form's filed, to be used when the page is loaded for medium selection."
             rtexprvalue="true" type="java.lang.String"%>

<%@attribute name="mediumFormatsData" required="false" description="Medium formats data response object."
             rtexprvalue="true" type="DataRetrievalResponse<MiscDataComponent>" %>
<%-- End of Attributes --%>

<%-- Page Body Content --%>
<jstlc:choose>
    <jstlc:when test="${data == null}">
        Failed to retrieve medium/media data object.
    </jstlc:when>

    <jstlc:when test="${data.requestResult && data.valid}">
        <jstlc:choose>
            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.SINGLE_RESOURCE %>">
                <table id="mainMediumTable" class="mainResourceDataTable">
                    <tr><td>Medium ID:</td><td>${data.resource.mediumData.resourceId}</td></tr>
                    <tr><td>Medium Name:</td><td>${data.resource.mediumName}</td></tr>
                    <tr><td>Medium Type:</td><td>${data.resource.deviceType}</td></tr>
                    <tr><td>Medium State:</td><td>${data.resource.mediumState}</td></tr>
                    <tr><td>Server ID:</td><td>${data.resource.mediumData.serverId}</td></tr>
                    <tr><td>Owner ID:</td><td>${data.resource.mediumData.ownerId}</td></tr>
                    <tr><td>Creation Date:</td><td>${data.resource.mediumData.dateCreated}</td></tr>
                    <tr><td>Permissions:</td><td>${data.resource.mediumData.permissions}</td></tr>
                    <tr><td>Details:</td>
                        <td><a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('otherInfoMediumTable');">View</a></td></tr>
                 </table>
                 <div id="mediumOperations">
                     <a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formModifyMedium');">Modify Medium</a><br>
                     <jstlc:if test="${type == 1}">
                     <a class="actionLink" href="manage_medium.jsp?action=10&amp;id=${data.resource.mediumData.resourceId}">Reset Disk</a><br>
                     <a class="actionLink" href="manage_medium.jsp?action=8&amp;id=${data.resource.mediumData.resourceId}">Compact Disk</a><br>
                     <a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formMergeDisks');">Merge Disks</a><br>
                     <a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formCloneDisk');">Clone Disk</a><br>
                     <a class="actionLink" href="noJavaScript.jsp"onclick="return changePopupVisibility('formResizeDisk');">Resize Disk</a><br>
                     <a class="actionLink" href="manage_medium.jsp?action=11&amp;type=${type}&amp;id=${data.resource.mediumData.resourceId}&amp;delete"
                        onclick="return resourceRemovalVerification('${data.resource.mediumName} (w/ storage)', this);">Remove Disk</a><br>
                     </jstlc:if>
                     <a class="actionLink" href="manage_medium.jsp?action=11&amp;type=${type}&amp;id=${data.resource.mediumData.resourceId}"
                        onclick="return resourceRemovalVerification('${data.resource.mediumName} (w/o storage)', this);">Close Disk</a><br>
                 </div>
                 <table id="otherInfoMediumTable" class="resourceDataTable">
                    <tr><td>Description:</td><td>${data.resource.description}</td></tr>
                    <tr><td>Location:</td><td>${data.resource.mediumLocation}</td></tr>
                    <tr><td>Host Drive:</td><td>${data.resource.hostDrive}</td></tr>
                    <tr><td>Physical Size:</td><td>${data.resource.physicalSize}B</td></tr>
                    <tr><td>Logical Size:</td><td>${data.resource.logicalSize}MB</td></tr>
                    <tr><td>Storage Format:</td><td>${data.resource.storageFormat}</td></tr>
                    <tr><td>Medium Format:</td><td>${data.resource.mediumFormat.name}</td></tr>
                    <tr><td>Type:</td><td>${data.resource.mediumType}</td></tr>

                    <jstlc:if test="${type == 1}">
                        <tr><td>Parent ID:</td><td>${data.resource.parentId}</td></tr>
                        <tr><td>Children IDs:</td><td>
                        <jstlc:forEach var="childId" items="${data.resource.childrenIds}">
                            ${childId} / <a href="media.jsp?type=${type}&amp;id=${childId}">View</a><br>
                        </jstlc:forEach>
                        </td></tr>
                        <tr><td>Base ID:</td><td>${data.resource.baseMediumId == data.resource.mediumData.resourceId
                                                    ? "(this)"
                                                    : data.resource.baseMediumId}</td></tr>
                        <tr><td>Read Only:</td><td>${data.resource.readOnly ? "Yes" : "No"}</td></tr>
                        <tr><td>Auto-Reset:</td><td>${data.resource.autoResetEnabled ? "Enabled" : "Disabled"}</td></tr>
                    </jstlc:if>
                    <tr><td>Last Error:</td><td>${data.resource.lastAccessError}</td></tr>
                    <tr><td>Machine IDs:</td><td>
                    <jstlc:forEach var="machineId" items="${data.resource.attachedMachinesIds}">
                        ${machineId} / <a href="machines.jsp?id=${machineId}">View</a><br>
                    </jstlc:forEach>
                    </td></tr>
                 </table>
                 <form class="resourceModification" id="formModifyMedium" action="manage_medium.jsp?action=5&amp;type=${type}&amp;id=${data.resource.mediumData.resourceId}" method="post">
                     <table>
                         <tr><td>Description:</td><td><input type="text" name="modify.medium.description" value="${data.resource.description}"></td>
                             <td><input type="submit" name="modify.medium.description.submit" value="Update"></td></tr>
                         <tr><td>Location:</td><td><input type="text" name="modify.medium.location" value="${data.resource.mediumLocation}"></td>
                             <td><input type="submit" name="modify.medium.location.submit" value="Update"></td></tr>
                         <tr><td>Medium Type:</td><td><select name="modify.medium.type">
                                  <jstlc:forEach var="mediumType" items="<%= MediumType.values() %>">
                                    <option value="${mediumType}" ${data.resource.mediumType == mediumType ? "selected" : ""}>${mediumType}</option>
                                  </jstlc:forEach></select></td>
                             <td><input type="submit" name="modify.medium.type.submit" value="Update"></td></tr>
                         <jstlc:if test="${type == 1}">
                         <tr><td>Auto-Reset:</td><td><input type="checkbox" name="modify.medium.autoreset" value="${data.resource.autoResetEnabled ? "checked" : ""}"></td>
                             <td><input type="submit" name="modify.medium.autoreset.submit" value="Update"></td></tr>
                         </jstlc:if>
                         <tr><td>Permissions:</td><td><input type="text" size="2" name="modify.medium.permissions" value="${data.resource.mediumData.permissions}"></td>
                             <td><input type="submit" name="modify.medium.permissions.submit" value="Update"></td></tr>
                         <tr><td>Owner ID:</td><td><input type="text" size="3" name="modify.medium.newOwner" value="${data.resource.mediumData.ownerId}"></td>
                             <td><input type="submit" name="modify.medium.newOwner.submit" value="Update"></td></tr>
                     </table>
                 </form>
                 <jstlc:if test="${type == 1}">
                     <form class="resourceModification" id="formMergeDisks" action="manage_medium.jsp?action=6&amp;id=${data.resource.mediumData.resourceId}" method="post">
                         Target Disk UUID: <input type="text" size="36" name="merge.disks.target"><br>
                         <input type="submit" value="Merge">
                     </form>
                     <form class="resourceModification" id="formCloneDisk" action="manage_medium.jsp?action=7&amp;id=${data.resource.mediumData.resourceId}" method="post">
                        <table>
                            <tr><td>Target Disk UUID:</td><td><input type="text" size="36" name="clone.disk.target"></td></tr>
                            <tr><td>Disk Variant:</td><td><select name="clone.disk.variant">
                                      <jstlc:forEach var="variant" items="<%= MediumVariant.values() %>">
                                        <option value="${variant}">${variant}</option>
                                      </jstlc:forEach></select></td></tr>
                            <tr><td>Parent Disk UUID:</td><td><input type="text" size="36" name="clone.disk.parent"></td></tr>
                            <tr><td><input type="submit" value="Clone"></td></tr>
                         </table>
                     </form>
                     <form class="resourceModification" id="formResizeDisk" action="manage_medium.jsp?action=9&amp;id=${data.resource.mediumData.resourceId}" method="post">
                         Disk Size: <input type="text" size="5" name="resize.disk.size" value="${data.resource.logicalSize}">MB<br>
                         <input type="submit" value="Resize">
                     </form>
                 </jstlc:if>
            </jstlc:when>

            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.MULTIPLE_RESOURCES %>">
                <div id="mediaSelectionElement">
                <table id="mediaTable" class="resourcesTable">
                    <tr><td>UUID</td><td>Name</td><td>Server ID</td><td>Owner ID</td><td></td><td></td></tr>
                <jstlc:choose>
                    <jstlc:when test="${targetFieldName != null}">
                        <jstlc:forEach var="medium" items="${data.resourcesList}">
                            <tr><td>${medium.mediumData.resourceId}</td><td>${medium.mediumName}</td><td>${medium.mediumData.serverId}</td><td>${medium.mediumData.ownerId}</td>
                                <td><a class="operationsLink" href="noJavaScript.jsp" onclick="return makeResourceSelection('${targetFieldName}', '${medium.mediumData.resourceId}');">Select</a></td><td></td>
                            </tr>
                        </jstlc:forEach>
                    </jstlc:when>
                    <jstlc:otherwise>
                        <jstlc:forEach var="medium" items="${data.resourcesList}">
                            <tr><td>${medium.mediumData.resourceId}</td><td>${medium.mediumName}</td>
                                <td><a href="servers.jsp?id=${medium.mediumData.serverId}">${medium.mediumData.serverId}</a></td>
                                <td><a href="users.jsp?id=${medium.mediumData.ownerId}">${medium.mediumData.ownerId}</a></td>
                                <td><a class="operationsLink" href="media.jsp?type=${type}&amp;id=${medium.mediumData.resourceId}">View</a></td>
                                <td><a class="operationsLink" href="manage_medium.jsp?action=11&amp;type=${type}&amp;id=${medium.mediumData.resourceId}"
                                       onclick="return resourceRemovalVerification('${medium.mediumName} (w/o storage)', this);">Remove</a></td>
                            </tr>
                        </jstlc:forEach>
                    </jstlc:otherwise>
                </jstlc:choose>
                </table>
                <script type="text/javascript">
                    var mediaPagination = new ElementsTablePagination('mediaTable', 'mediaPagination');
                </script>
                Page Size: <input type="text" size="2" id="itemsPerPage" value="20">
                <a class="pageSizeUpdateLink" href="noJavaScript.jsp"
                   onclick="return mediaPagination.setMaxNumberOfItemsPerPage(document.getElementById('itemsPerPage').value);">Update</a>
                <jstlc:if test="${targetFieldName != null}">
                    <br><a class="actionLink" href="noJavaScript.jsp" onclick="self.close();">Close</a>
                    <script type="text/javascript">
                        selectionWindowModification('mediaSelectionElement');
                    </script>
                </jstlc:if>
                </div>
                <div class="creationOperations">
                <jstlc:choose>
                    <jstlc:when test="${type == 1}">
                        <a class="creationLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formCreateHardDisk');">Create Hard Disk</a>
                        <a class="creationLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formOpenHardDisk');">Open Hard Disk</a>
                        <form class="resourceCreationElement" id="formCreateHardDisk" action="manage_medium.jsp?action=1" method="post">
                            <table>
                                <tr><td>Create Disk:</td></tr>
                                <tr><td>Disk Type:</td><td><select name="create.disk.type" onchange="updateDiskTypeFields('formCreateHardDisk', 'create.disk');">
                                             <option value="1">Normal</option>
                                             <option value="2">Differencing</option></select></td></tr>
                                <tr><td>Disk Name:</td><td><input type="text" name="create.disk.location"></td></tr>
                                <tr><td>Disk Size:</td><td><input type="text" size="3" name="create.disk.size"></td></tr>
                                <tr><td>Format:</td><td><select name="create.disk.format"><option value="0" selected disabled>Select</option>
                                    <jstlc:choose>
                                        <jstlc:when test="<%= mediumFormatsData != null && mediumFormatsData.getRequestResult() %>">
                                        <jstlc:forEach var="mediumFormat" items="<%= mediumFormatsData.getResource().getMediumFormats() %>">
                                            <option value="${mediumFormat.formatId}">${mediumFormat.formatName}</option>
                                        </jstlc:forEach>
                                        </jstlc:when>
                                        <jstlc:otherwise>
                                        <option value="0">N/A</option>
                                        </jstlc:otherwise>
                                    </jstlc:choose>
                                    </select></td></tr>
                                <tr><td>Variant:</td><td><select name="create.disk.variant">
                                              <jstlc:forEach var="variant" items="<%= MediumVariant.values() %>">
                                                <option value="${variant}">${variant}</option>
                                              </jstlc:forEach></select></td></tr>
                                <tr><td>Permissions:</td><td><input type="text" size="2" name="create.disk.permissions"></td></tr>
                                <tr><td>Parent UUID:</td><td><input type="text" size="36" name="create.disk.parentId" disabled></td></tr>
                                <tr><td><input type="submit" value="Create"></td></tr>
                            </table>
                        </form>
                        <form class="resourceCreationElement" id="formOpenHardDisk" action="manage_medium.jsp?action=2" method="post">
                            <table>
                                <tr><td>Open Disk:</td></tr>
                                <tr><td>Disk Location:</td><td><input type="text" name="open.disk.location"></td></tr>
                                <tr><td>Access Mode:</td><td><select name="open.disk.accessMode">
                                              <jstlc:forEach var="accessMode" items="<%= AccessMode.values() %>">
                                                <option value="${accessMode}">${accessMode}</option>
                                              </jstlc:forEach></select></td></tr>
                                <tr><td>Permissions:</td><td><input type="text" size="2" name="open.disk.permissions"></td></tr>
                                <tr><td><input type="submit" value="Create"></td></tr>
                            </table>
                        </form>
                    </jstlc:when>
                    <jstlc:when test="${type == 2}">
                        <a class="creationLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formOpenDVD');">Open DVD</a>
                        <form class="resourceCreationElement" id="formOpenDVD" action="manage_medium.jsp?action=3" method="post">
                            <table>
                                <tr><td>Open DVD:</td></tr>
                                <tr><td>DVD Name:</td><td><input type="text" name="open.dvd.location"></td></tr>
                                <tr><td>Permissions:</td><td><input type="text" size="2" name="open.dvd.permissions"></td></tr>
                                <tr><td><input type="submit" value="Create"></td></tr>
                            </table>
                        </form>
                    </jstlc:when>
                    <jstlc:when test="${type == 3}">
                        <a class="creationLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formOpenFDD');">Open Floppy Disk</a>
                        <form class="resourceCreationElement" id="formOpenFDD" action="manage_medium.jsp?action=4" method="post">
                            <table>
                                <tr><td>Open Floppy:</td></tr>
                                <tr><td>FDD Name:</td><td><input type="text" name="open.fdd.location"></td></tr>
                                <tr><td>Permissions:</td><td><input type="text" size="2" name="open.fdd.permissions"></td></tr>
                                <tr><td><input type="submit" value="Create"></td></tr>
                            </table>
                        </form>
                    </jstlc:when>
                </jstlc:choose>
                </div>
            </jstlc:when>

            <jstlc:otherwise>
                Invalid resource request type: ${data.resourceRequestType}.
            </jstlc:otherwise>
        </jstlc:choose>
    </jstlc:when>

    <jstlc:otherwise>
        Error: ${data.exception.exceptionMessage}
    </jstlc:otherwise>
</jstlc:choose>
<%-- End of Page Body Content --%>